package chixing.day16;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class OutputStream_Demo {
    public static void main(String[] args) {
        //1.创建字节写入流
        OutputStream os = null;
        try {
            os = new FileOutputStream("E:\\2025-cx\\project\\PJ\\J01\\Javase\\Temp\\byte_hello.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        String str ="The value is used for character storage.";
        try {
            //2.写操作，写入时是按1字节，1字节 写入所以要转化为他的字节数组
            os.write(str.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {//释放资源 节省内存空间
            if (os != null) {//判断os是否为null，有没有被创建，可能在创建时出现异常，导致os为null，当os为null时在调用对象会出现空指针异常
                try {
                    //3.关闭流
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
